﻿using System.Windows;
using System.Windows.Media.Media3D;
using System.Diagnostics;
using LFSRecord2.Model.Undo;

namespace LFSRecord2.Model
{
    /// <summary>
    /// Class for Point3D keyframes
    /// </summary>
    public class CustomPoint3DKeyFrame : CustomKeyFrame
    {
        private Point3D _value;
        public Point3D Value
        {
            get { return (Point3D)_value; }
            set
            {
                _value = value;

                if (ParentProperty != null && ParentProperty.PropertyName == "Orientation")
                {
                    if (_value.X < 0)
                        _value.X += 360;
                    else if (_value.X >= 360)
                        _value.X -= 360;

                    if (_value.Y < 0)
                        _value.Y += 360;
                    else if (_value.Y >= 360)
                        _value.Y -= 360;

                    if (_value.Z < 0)
                        _value.Z += 360;
                    else if (_value.Z >= 360)
                        _value.Z -= 360;
                }

                OnPropertyChanged("Value");
            }
        }
        public double xValue
        {
            get { return _value.X; }
            set
            {
                Value = new Point3D(value, _value.Y, _value.Z);
            }
        }
        public double yValue
        {
            get { return _value.Y; }
            set
            {
                Value = new Point3D(_value.X, value, _value.Z);
            }
        }
        public double zValue
        {
            get { return _value.Z; }
            set
            {
                Value = new Point3D(_value.X, _value.Y, value);
            }
        }

        private Point3D _bezierControlIn = new Point3D();
        public Point3D BezierControlIn
        {
            get { return (Point3D)_bezierControlIn; }
            set 
            {
                //Debug.WriteLine("B-IN : {0},{1},{2}", value.X, value.Y, value.Z);

                _bezierControlIn = value;
                OnPropertyChanged("BezierControlIn");
            }
        }
        private Point3D _bezierControlOut = new Point3D();
        public Point3D BezierControlOut
        {
            get { return (Point3D)_bezierControlOut; }
            set 
            {
                //Debug.WriteLine("B-OUT : {0},{1},{2}", value.X, value.Y, value.Z);

                _bezierControlOut = value;
                OnPropertyChanged("BezierControlOut");
            }
        }

        public CustomPoint3DKeyFrame(int frame, Point3D newValue)
            : base(frame)
        {
            _value = newValue;
            KeyFrameType = CustomKeyFrameTypes.Point3D;
            
            InterpolationTypeIn = LfsRecordController.project.DefaultInterpolationType;
            InterpolationTypeOut = LfsRecordController.project.DefaultInterpolationType;
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }

        public CustomPoint3DKeyFrame Clone()
        {
            CustomPoint3DKeyFrame kf = new CustomPoint3DKeyFrame(FrameNum, new Point3D(xValue, yValue, zValue));
            kf.BezierControlIn = new Point3D(BezierControlIn.X, BezierControlIn.Y, BezierControlIn.Z);
            kf.BezierControlOut = new Point3D(BezierControlOut.X, BezierControlOut.Y, BezierControlOut.Z);
            kf.Options = Options;
            kf.KeyFrameType = KeyFrameType;
            kf.IsSelected = IsSelected;
            kf.KeyImageType = KeyImageType;
            kf.KeyFrameIndex = KeyFrameIndex;
            kf.EaseInValue = EaseInValue;
            kf.EaseOutValue = EaseOutValue;
            kf.InterpolationTypeIn = InterpolationTypeIn;
            kf.InterpolationTypeOut = InterpolationTypeOut;

            return kf;
        }

        public override KeyFrameDataBase DataStructure()
        {
            KeyFramePoint3DData keyFrameData = new KeyFramePoint3DData()
            {
                UniqueID = UniqueID,
                KeyFrameType = KeyFrameType,
                ParentPropertyIndex = ParentProperty.PropertyIndex,
                ParentLayerIndex = ParentLayer.LayerIndex,
                IsSelected = IsSelected,
                FrameNum = FrameNum,
                KeyFrameIndex = KeyFrameIndex,
                EaseInValue = EaseInValue,
                EaseOutValue = EaseOutValue,
                InterpolationTypeIn = InterpolationTypeIn,
                InterpolationTypeOut = InterpolationTypeOut,
                Options = Options,
                DistanceToNextKeyFrame = DistanceToNextKeyFrame,
                BezierTime = BezierTime,

                Value = _value,
                BezierControlIn = BezierControlIn,
                BezierControlOut = BezierControlOut
            };

            return keyFrameData;
        }
    }
}
